package zjc.bookkeeping.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import zjc.bookkeeping.R;
import zjc.bookkeeping.tools.ToolsClass;

public class DBOpenHelper extends SQLiteOpenHelper {
    public DBOpenHelper(@Nullable Context context) {
        super(context,"account_book.db" , null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //创建用户表
        String sql = "create table user(user_id integer,user_name vachar(60),user_photo vachar(60),user_phone vachar(60),user_qq varchar(40),user_wx vachar(60),user_zfb vachar(60))";
        sqLiteDatabase.execSQL(sql);
//        insertUser(sqLiteDatabase);
//        创建消费分类表
        sql = "create table consumptionclass(consumption_class_id integer primary key autoincrement,consumption_class_name varchar(10)," +
                "consumption_class_image vachar(60),consumption_class_kind integer)";
        sqLiteDatabase.execSQL(sql);
        insertConsumptionClass(sqLiteDatabase);
//        创建账本表
        sql = "create table account(account_id integer primary key autoincrement, account_name varchar(10), account_time varchar(60),account_kind integer,account_key varchar(60))";
        sqLiteDatabase.execSQL(sql);
        addAccount(sqLiteDatabase);
//        创建账本记录表
        sql = "create table consumption(consumption_id integer primary key autoincrement, consumption_class_name varchar(10)," +
                "consumption_remarks varchar(80), consumption_money float, consumption_business varchar(10), account_id varchar(10), consumption_method varchar(10)," +
                " consumption_time varchar(60), consumption_year integer, consumption_month integer, consumption_day integer, consumption_kind integer,consumption_key integer)";
        sqLiteDatabase.execSQL(sql);
//        创建账户表
        sql = "create table method(method_id integer primary key autoincrement, method_name varchar(10),method_image vachar(60), method_money float,method_remark vachar(60),method_card vachar(60))";
        sqLiteDatabase.execSQL(sql);
        insertMethod(sqLiteDatabase);
//        创建商家表
        sql = "create table business(business_id integer primary key autoincrement, business_name varchar(10))";
        sqLiteDatabase.execSQL(sql);
//        创建每月消费额度表
        sql = "create table quota(quota_id integer primary key autoincrement,quota_money float,quota_time varchar(60))";
        sqLiteDatabase.execSQL(sql);
        //创建选择中账本
        sql = "create table selectAccount(selectPos integer,accountId integer)";
        sqLiteDatabase.execSQL(sql);
        insertSelectPos(sqLiteDatabase);
    }

    private void insertUser(SQLiteDatabase sqLiteDatabase) {
        String sql = "insert into user (user_id, user_name, user_phone) values(?,?,?)";
        sqLiteDatabase.execSQL(sql,new Object[]{12,"手机用户","17807942069"});
    }

    private void insertSelectPos(SQLiteDatabase sqLiteDatabase){
        String sql = "insert into selectAccount(selectPos,accountId) values(?,?)";
        sqLiteDatabase.execSQL(sql,new Object[]{0,1});
    }
    private void insertMethod(SQLiteDatabase sqLiteDatabase){
        String sql = "insert into method (method_name, method_image, method_money) values(?,?,?)";
        sqLiteDatabase.execSQL(sql,new Object[]{"现金","a_list_xj",100});
        sqLiteDatabase.execSQL(sql,new Object[]{"支付宝","a_list_zfb",1500});
        sqLiteDatabase.execSQL(sql,new Object[]{"微信","a_list_wx",150});
        sqLiteDatabase.execSQL(sql,new Object[]{"QQ钱包","a_list_qqqb",15});
    }
    private void addAccount(SQLiteDatabase sqLiteDatabase) {
        String sql = "insert into account (account_name,account_time,account_kind) values(?,?,?)";
        sqLiteDatabase.execSQL(sql, new Object[]{"日常账本",ToolsClass.getYMDTime(0),0});
    }

    private void insertConsumptionClass(SQLiteDatabase sqLiteDatabase) {
        String sql = "insert into consumptionclass (consumption_class_name,consumption_class_image,consumption_class_kind) values (?,?,?)";
        sqLiteDatabase.execSQL(sql,new Object[]{"娱乐", "entertainment", 0});
        sqLiteDatabase.execSQL(sql,new Object[]{"出行", "travel", 0});
        sqLiteDatabase.execSQL(sql,new Object[]{"学习", "study", 0});
        sqLiteDatabase.execSQL(sql,new Object[]{"餐饮", "restaurant", 0});
        sqLiteDatabase.execSQL(sql,new Object[]{"日用品", "necessities", 0});
        sqLiteDatabase.execSQL(sql,new Object[]{"其他支出", "outcome_other", 0});

        sqLiteDatabase.execSQL(sql,new Object[]{"理财", "finance", 1});
        sqLiteDatabase.execSQL(sql,new Object[]{"奖金", "bonus", 1});
        sqLiteDatabase.execSQL(sql,new Object[]{"利息", "interest", 1});
        sqLiteDatabase.execSQL(sql,new Object[]{"生活费", "living_expenses", 1});
        sqLiteDatabase.execSQL(sql,new Object[]{"红包", "red_envelopes", 1});
        sqLiteDatabase.execSQL(sql,new Object[]{"工资", "wages", 1});
        sqLiteDatabase.execSQL(sql,new Object[]{"其他收入", "income_other", 1});

        sqLiteDatabase.execSQL(sql, new Object[]{"转账", "transfer",2});
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
